//
// Unihan Input Method
//
// Copyright © 2002-2007 Sun Wah Linux Ltd.
//
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
// USA
//
// $Id: utils.c 757 2007-05-10 08:19:31Z roger $
//

/* FILE : utils.c */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>
#include <string.h>
#include <iconv.h>

#include <unihan_im.h>
#include <standard_im.h>

#include "utils.h"
#include "stdif.h"

void
free_ime_string(IME_String *ime_string)
{
	if (!ime_string)
		return;
	//if (ime_string->string)
	//	free(ime_string->string);
	if (ime_string->attributes)
		free(ime_string->attributes);
	free(ime_string);
}

IME_Attribute *
ime_attributes_ndup(IME_Attribute *ime_attr, int nr)
{
	IME_Attribute *attr;

	attr = (IME_Attribute *)malloc(nr * sizeof(IME_Attribute));
	memcpy(attr, ime_attr, nr * sizeof(IME_Attribute));
	return attr;
}

void
free_ime_candidates_list(IME_CandidatesList *ime_CL)
{
	IME_Candidate *c;
	int i;

	if (!ime_CL)
		return;
	c = ime_CL->candidates;

	for(i=0; i<ime_CL->nr_candidates; i++) {
		//if (c[i].label)
		//	free(c[i].label);
		//if (c[i].content.string)
		//	free(c[i].content.string);
		if (c[i].content.attributes)
			free(c[i].content.attributes);
	}
	free(c);
	free(ime_CL);
}

IME_CandidatesList *
ime_candidates_list_dup(const IME_CandidatesList *ime_CL)
{
	IME_CandidatesList *cl;
	IME_Candidate *c;
	int i;

	cl = (IME_CandidatesList *)malloc(sizeof(IME_CandidatesList));
	memcpy(cl, ime_CL, sizeof(IME_CandidatesList));

	cl->candidates = c = (IME_Candidate *)malloc(sizeof(IME_Candidate) *
			ime_CL->nr_candidates);
	memcpy(c, ime_CL->candidates,
			sizeof(IME_Candidate) *ime_CL->nr_candidates);
	
	for (i=0; i<ime_CL->nr_candidates; i++) {
		c[i].label = strdup(ime_CL->candidates[i].label);
		c[i].content.string =
				strdup(ime_CL->candidates[i].content.string);
		c[i].content.attributes = ime_attributes_ndup(
				ime_CL->candidates[i].content.attributes,
				ime_CL->candidates[i].content.nr_attributes);
	}
	return cl;
}

char *
encoding_to_utf8(UnihanIM *uim, const char *orig)
{
	StdifDataPerDesktop *dd = (StdifDataPerDesktop *)uim->desktopUserData;
	char *u8 = NULL, *u8_head, *enc;
	size_t ibytes, obytes, orig_len;

	enc = orig;
	orig_len = strlen(orig);
	ibytes = orig_len;
	obytes = orig_len * 3;
	uim->m->mem_alloc(uim, obytes, (void **)&u8);
	u8_head = u8;
#if DEBUG
	fprintf(stderr, "STD IF: encoding_to_utf8(): ibytes = %d  obytes = %d\n", ibytes, obytes);
#endif

	iconv(dd->iconv, &enc, &ibytes, &u8, &obytes);
	return u8_head;
}

// vi:ts=4:nowrap:cin:expandtab
